import { Define, EntityObjectFactory } from 'tiny-entity2';
const EntityObjectType = EntityObjectFactory.GetEntityObjectType(process.env.dataBaseType || 'mysql');
import { OrderItem } from './orderItem';
import { OrderDiscount } from './orderDiscount';
import { OrderPayment } from './orderPayment';
import { OrderCounter } from './orderCounter';

@Define.Table({ TableName: "orders" })
export class Order extends EntityObjectType<Order>{
    @Define.PrimaryKey()
    id: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR,
        NotAllowNULL: true
    })
    orderNo: string;

    /**
     * 订单流水号
     * 
     * @type {string}
     * @memberof Order
     */
    @Define.Column({
        DataType: Define.DataType.VARCHAR
    })
    serialNo: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR,
        NotAllowNULL: true
    })
    orderIdentity: string;

    @Define.Column({
        DataType: Define.DataType.BIGINT,
        NotAllowNULL: true
    })
    createTime: number;

    @Define.Column({
        DataType: Define.DataType.BIGINT,
        IsIndex: true
    })
    closeTime: number;

    @Define.Column({
        DataType: Define.DataType.BIGINT
    })
    reportTime: number;

    @Define.Column({
        DataType: Define.DataType.VARCHAR,
        NotAllowNULL: true,
        IsIndex: true
    })
    status: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR,
        NotAllowNULL: true,
        IsIndex: true
    })
    type: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR
    })
    note: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR,
        IsIndex: true
    })
    refOrderId: string;

    /**
     * 原价（所有商品原价总和）
     *
     * @type {number}
     * @memberof Order
     */
    @Define.Column({
        DataType: Define.DataType.Decimal,
        DefaultValue: 0,
        DataLength: 10,
        DecimalPoint: 3
    })
    subTotal: number;


    /**
     * 销售金额，所有商品销售金额之和。（处理套餐价格）
     *
     * @type {number}
     * @memberof Order
     */
    @Define.Column({
        DataType: Define.DataType.Decimal,
        DefaultValue: 0,
        DataLength: 10,
        DecimalPoint: 3
    })
    amount: number;

    /**
     * 订单折扣金额之和
     *
     * @type {number}
     * @memberof Order
     */
    @Define.Column({
        DataType: Define.DataType.Decimal,
        DefaultValue: 0,
        DataLength: 10,
        DecimalPoint: 3
    })
    discount: number;

    /**
     * 待支付金额
     * 
     * @type {number}
     * @memberof Order
     */
    @Define.Column({
        DataType: Define.DataType.Decimal,
        DefaultValue: 0,
        DataLength: 10,
        DecimalPoint: 3
    })
    remainAmount: number;

    /**
     * 已支付的金额
     * 
     * @type {number}
     * @memberof Order
     */
    @Define.Column({
        DataType: Define.DataType.Decimal,
        DefaultValue: 0,
        DataLength: 10,
        DecimalPoint: 3
    })
    paidAmount: number;

    @Define.Column({
        DataType: Define.DataType.VARCHAR,
        NotAllowNULL: true
    })
    operatorId: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR,
        NotAllowNULL: true
    })
    operatorName: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR
    })
    cashierId: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR
    })
    cashierName: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR
    })
    memberId: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR
    })
    memberName: string;

    /**
     * 会员来源，用于标记第三方平台会员
     *
     * @type {string}
     * @memberof Order
     */
    @Define.Column({
        DataType: Define.DataType.VARCHAR
    })
    memberSource: string;

    /**
     * 反结算次数
     *
     * @type {number}
     * @memberof Order
     */
    @Define.Column({
        DataType: Define.DataType.INT
    })
    counterTimes: number;

    /**
     *打印次数
     *
     * @type {number}
     * @memberof Order
     */
    @Define.Column({
        DataType: Define.DataType.INT
    })
    printTimes: number;

    @Define.Column({
        DataType: Define.DataType.VARCHAR
    })
    tableServiceLogId: string;

    /**
     * 发票金额
     *
     * @type {number}
     * @memberof Order
     */
    @Define.Column({
        DataType: Define.DataType.Decimal,
        DefaultValue: 0
    })
    invoiceAmount: number = 0;

    /**
     * 开票备注
     *
     * @type {string}
     * @memberof Order
     */
    @Define.Column({
        DataType: Define.DataType.VARCHAR,
    })
    invoiceNote: string;

    @Define.Column({
        DataType: Define.DataType.INT,
        DataLength: 10,
        DefaultValue: 0
    })
    customerCount: number;

    /**
     * 订单涞源，用于标记饿了么外卖， 美团外卖，扫码点餐等外部订单。
     *
     * @type {string}
     * @memberof Order
     */
    @Define.Column({
        DataType: Define.DataType.VARCHAR,
    })
    source: string;

    /**
     * 订单外卖的状态（ordered, refund, cancelled, closed）
     *
     * @type {string}
     * @memberof Order
     */
    @Define.Column({
        DataType: Define.DataType.VARCHAR,
    })
    deliveryStatus: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR,
        NotAllowNULL: true,
        IsIndex: true
    })
    storeId: string;

    @Define.Column({
        DataType: Define.DataType.JSON,
    })
    extend: any;

    @Define.Column({
        DataType: Define.DataType.BIGINT
    })
    version: number;

    @Define.Mapping({
        Mapping: 'OrderItem',
        MappingKey: 'masterOrderId'
    })
    orderItems: OrderItem[];

    @Define.Mapping({
        Mapping: 'OrderDiscount',
        MappingKey: 'orderId'
    })
    orderDiscounts: OrderDiscount[];

    @Define.Mapping({
        Mapping: 'OrderPayment',
        MappingKey: 'orderId'
    })
    orderPayments: OrderPayment[];

    @Define.Mapping({
        Mapping: 'OrderCounter',
        MappingKey: 'orderId'
    })
    orderCounters: OrderCounter[];
}

export const OrderType = {
    Sales: 'sales',
    PreOrder: 'preOrder',
    Refund: 'refund',
    Remind: 'remind',
    CheckOut: 'checkout',
    MemberRecharge: 'memberRecharge',
    Delivery: 'delivery'
}

export const OrderDeliveryStatus = {
    Ordered: 'ordered',
    Refund: 'refund',
    Cancelled: 'cancelled',
    Closed: 'closed'
}

export const OrderStatus = {
    Init: 'init',
    Ordered: 'ordered',
    Closed: 'closed',
    Cancelled: 'cancelled',
    Settlement: 'settlement',
    PreSettlement: 'presettlement',
    Merged: 'merged',
    Refund: 'refund'
}